<template>
  <!-- 扫码商品入库 -->
  <view class="stock-entry">
    <view class="info-card">
      <up-image :show-loading="true" :src="shopData.image" width="48px" height="48px"></up-image>
      <view class="product-info">
        <view class="product-name">{{ shopData.name }}</view>
        <view class="product-code">款号：{{ shopData.productCode }}</view>
        <view class="product-spec">规格：{{ shopData.spec }}</view>
      </view>
    </view>
    <view class="store-card">
      <view class="store-name">{{ shopName }}</view>
      <view class="quantity-card">
        <view class="label">库存数量</view>
        <view class="number-input">
          <view class="btn minus" :disabled="shopData.totalQuantity <= 0" @click="decreaseQuantity">-</view>
          <input type="number" v-model="shopData.totalQuantity" class="input" />
          <view class="btn plus" @click="increaseQuantity">+</view>
        </view>
      </view>
    </view>
    <view class="footer" >
      <button class="confirm-btn" @click="handleCancel">删除</button>
      <button class="confirm-btn1" @click="handleConfirm" :disabled="loading">
        <up-loading-icon class="loading" color="#fff" size="16" :show="loading"></up-loading-icon>  
        确定
      </button>
    </view>
    <!-- 入库成功 -->
    <view class="success-page" v-if="showSuccessPage">
      <view class="success-icon">
        <u-icon name="checkmark-circle" color="rgba(43, 164, 113, 1)" size="80"></u-icon>
      </view>
      <text class="success-text">修改成功</text>
      <view class="button-group">
        <button class="action-btn secondary-btn" @click="handleComplete">完成</button>
      </view>
    </view>

    <!-- 编辑往年库存按钮 -->
    <view class="edit-last-year" @click="showLastYearModal = true">
      往年库存
    </view>

    <!-- 编辑往年库存弹窗 -->
    <u-popup :show="showLastYearModal" @close="showLastYearModal = false" mode="bottom" round="10">
      <view class="last-year-modal">
        <view class="flex justify-between items-center f-24 px-24 py-24">
            <view class="popupcancel" @click="showLastYearModal = false">取消</view>
            <view class="popupTit">编辑往年库存</view>
            <view class="popupconfirm" @click="saveLastYearStock">确认</view>
        </view>
        <view class="modal-content">
          <input type="number" v-model="lastYearStock" class="last-year-input" placeholder="请输入往年库存数量"/>
        </view>
        <!-- <view class="modal-footer">
          <button class="save-btn" @click="saveLastYearStock" :disabled="loading" >
            <up-loading-icon class="loading" color="#fff" size="16" :show="loading"></up-loading-icon>  
            保存
          </button>
        </view> -->
      </view>
    </u-popup>
  </view>
</template>

<script setup>
import { ref } from "vue";
import { shopApi } from "@/api";
import { getStorage, setStorage } from "@/utils/storage";

const shopName = ref("");
const quantity = ref(0);
const skuId = ref(1);
const shopData = ref({});
let showSuccessPage = ref(false);
const showLastYearModal = ref(false);
const lastYearStock = ref("");
const loading = ref(false);


const decreaseQuantity = () => {
  if (shopData.value.totalQuantity > 0) {
    shopData.value.totalQuantity--;
  }
};

const increaseQuantity = () => {
  shopData.value.totalQuantity++;
};

const saveLastYearStock = () => {
  // TODO: 调用保存往年库存的API
  if(lastYearStock.value == ''){ 
    uni.showToast({
      title: '请输入往年库存数量',
      icon: 'none'
    });
    return
  }
  loading.value = true;
  shopApi.SHOPSTOCKEDITSHOPLASTYEARSTOCK({
    shopStockId: shopData.value.shopStockRecordId,
    lastYearStock: lastYearStock.value
  }).then(res => {
    loading.value = false;
    showLastYearModal.value = false;
    if (res.code == 200) {
      uni.showToast({
          title: '保存成功',
          icon: 'success'
        });
      }else{
        uni.showToast({
          title: res.errMsg,
          icon: 'none'
        });
      }
  })
};

onLoad((options) => {
  console.log(JSON.parse(options.shopData),'111')
  if(options.type==2){
    shopData.value = JSON.parse(options.shopData);
    shopData.value.totalQuantity=JSON.parse(options.shopData).inStockQuantity?JSON.parse(options.shopData).inStockQuantity:1
    shopData.value.name =JSON.parse(options.shopData).productName
    shopData.value.shopStockRecordId = JSON.parse(options.shopData).shopStockRecordId
    shopData.value.image = JSON.parse(options.shopData).image
    lastYearStock.value = JSON.parse(options.shopData).lastYearStock
  }
  shopName.value = getStorage("shopName");
});

const handleConfirm = () => {
  loading.value = true;
  
  shopApi.SHOPSTOCKEDITRECORD({
      shopStockRecordId: shopData.value.shopStockRecordId,
      inStockQuantity: shopData.value.totalQuantity
    })
    .then((res) => {
    loading.value = false;
      if (res.code != 200) {
        uni.showToast({
          title: res.errMsg,
          icon: "none"
        });
        return;
      }
      showSuccessPage.value = true;
    });
};
const handleCancel = () => {
  uni.showModal({
    title: "提示",
    content: "确定删除该商品库存吗？",
    success: function (res) {
      if (res.confirm) {
        shopApi
          .SHOPSTOCKDELRECORD({
            id: shopData.value.shopStockRecordId
          })
          .then((res) => {
            if (res.code != 200) {
              uni.showToast({
                title: res.errMsg,
                icon: "none"
              });
              return;
            }
            uni.navigateBack();
          });
        }
      }
    })
}
const handleContinue = () => {
  showSuccessPage.value = false;
  // 返回上一级
  uni.navigateBack();
};

const handleComplete = () => {
  
  uni.navigateBack({
    delta: 1 // Navigate back to the main page
  });
};
</script>
<style lang="scss">
page {
  background-color: #f3f3f3;
}
</style>
<style lang="scss" scoped>
.stock-entry {
  padding: 24rpx 24rpx 120rpx 24rpx;
  box-sizing: border-box;
  position: relative;
  box-sizing: border-box;
  .info-card {
    background-color: #fff;
    border-radius: 12rpx;
    padding: 24rpx;
    box-sizing: border-box;
    margin-bottom: 20rpx;
    display: flex;
  }

  .info-card {
    .product-info {
      margin-left: 20rpx;

      .product-name {
        font-size: 28rpx;
        color: #333;
        margin-bottom: 10rpx;
      }

      .product-code,
      .product-spec {
        font-size: 24rpx;
        color: #666;
        margin-bottom: 6rpx;

        &:last-child {
          margin-bottom: 0;
        }
      }
    }
  }

  .store-card {
    font-size: 28rpx;
    color: #333;
    background-color: #fff;
    border-radius: 12rpx;
    padding: 24rpx;
    margin-bottom: 20rpx;
    box-sizing: border-box;

    .store-name {
      font-size: 12px;
      font-weight: 400;
      line-height: 20px;
      color: rgba(0, 0, 0, 0.9);
      margin-bottom: 24rpx;
    }
  }

  .quantity-card {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .label {
      font-size: 28rpx;
      color: #333;
      margin-bottom: 20rpx;
      display: block;
    }

    .number-input {
      display: flex;
      align-items: center;
      justify-content: center;

      .btn {
        width: 56rpx;
        height: 56rpx;
        display: flex;
        align-items: center;
        justify-content: center;
        background-color: #f5f5f5;
        border: none;
        border-radius: 8rpx;

        &:active {
          opacity: 0.8;
        }

        &.disabled {
          opacity: 0.5;
          pointer-events: none;
        }

        text {
          font-size: 36rpx;
          color: #333;
        }
      }

      .input {
        width: 120rpx;
        height: 56rpx;
        text-align: center;
        margin: 0 20rpx;
        background-color: #f5f5f5;
        border: none;
        border-radius: 8rpx;
        font-size: 28rpx;
      }
    }
  }

  .footer {
    position: fixed;
    bottom: 20rpx;
    left: 0;
    right: 0;
    padding: 24rpx;
    box-sizing: border-box;
    background-color: #fff;
    box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);
    box-sizing: border-box;
    z-index: 9;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .confirm-btn {
      flex: 1;
      width: 100%;
      height: 80rpx;
      line-height: 80rpx;
      color: rgba(214, 142, 68, 1);
      border: 1rpx solid rgba(214, 142, 68, 1);
      border-radius: 40rpx;
      font-size: 32rpx;

      &:active {
        opacity: 0.9;
      }
    }

    .confirm-btn1 {
      flex:2;
      width: 100%;
      height: 80rpx;
      line-height: 80rpx;
      background-color: rgba(214, 142, 68, 1);
      color: #fff;
      border: none;
      border-radius: 40rpx;
      font-size: 32rpx;
      margin-left: 24rpx;
      display: flex;
      justify-content: center;
      &:active {
        opacity: 0.9;
      }
    }
  }

  .success-page {
    position: fixed;
    top: 0;
    left: 0;
    background-color: #fff;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 120rpx 40rpx;
    box-sizing: border-box;
    z-index: 99;
    width: 100%;
    height: 100vh;
    .success-icon {
      margin-bottom: 40rpx;
    }

    .success-text {
      font-size: 36rpx;
      color: #333;
      font-weight: bold;
      margin-bottom: 100rpx;
    }

    .button-group {
      width: 100%;
      display: flex;
      flex-direction: column;
      gap: 20rpx;
      .action-btn {
        width: 100%;
        height: 88rpx;
        line-height: 88rpx;
        border-radius: 44rpx;
        font-size: 32rpx;
        border: none;

        &.primary-btn {
          background-color: rgba(214, 142, 68, 1);
          color: #fff;
        }

        &.secondary-btn {
          background-color: rgba(253, 236, 219, 1);
          color: rgba(214, 142, 68, 1);
        }
      }
    }
  }

  .edit-last-year {
    position: fixed;
    right: 30rpx;
    bottom: 250rpx;
    background-color: rgba(214, 142, 68, 1);
    color: #fff;
    padding: 20rpx 30rpx;
    border-radius: 40rpx;
    font-size: 28rpx;
    z-index: 9;
  }

  .last-year-modal {
    background: #fff;
    padding: 40rpx;
    border-radius: 20rpx;
    .popupcancel{
        font-family: PingFang SC;
        font-size: 14px;
        font-weight: 400;
        line-height: 22px;
        text-align: left;
        color: rgba(0, 0, 0, 0.9);
    }
    .popupconfirm{
        font-family: PingFang SC;
        font-size: 14px;
        font-weight: 600;
        line-height: 22px;
        text-align: right;
        color: rgba(193, 122, 50, 1)
    }
    .popupTit{
        font-family: PingFang SC;
        font-size: 18px;
        font-weight: 600;
        line-height: 26px;
        text-align: right;
        color:rgba(0, 0, 0, 0.9);
    }

    .modal-content {
      margin-bottom: 40rpx;

      .last-year-input {
        height: 80rpx;
        background: #f5f5f5;
        border-radius: 8rpx;
        padding: 0 20rpx;
        font-size: 28rpx;
      }
    }

    .modal-footer {
      .save-btn {
        width: 100%;
        height: 80rpx;
        line-height: 80rpx;
        background-color: rgba(214, 142, 68, 1);
        color: #fff;
        border: none;
        border-radius: 40rpx;
        font-size: 32rpx;
        display: flex;
        justify-content: center;
      }
    }
  }
}
</style>
